package com.gis.handler;

import com.gis.exception.CustomException;
import com.gis.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理器，处理项目中抛出的业务异常
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseResult exceptionHandler(CustomException ex){
        return ResponseResult.fail(ex.getMessage());
    }

    /**
     * 处理反射异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ResponseResult exceptionHandler(NoSuchMethodException ex){
        return ResponseResult.fail("反射异常"+ex.getMessage());
    }
    @ExceptionHandler
    public ResponseResult exceptionHandler(InvocationTargetException ex){
        return ResponseResult.fail("反射异常"+ex.getMessage());
    }
    @ExceptionHandler
    public ResponseResult exceptionHandler(IllegalAccessException ex){
        return ResponseResult.fail("反射异常"+ex.getMessage());
    }

}
